<template>
    <div id="app" style="width: 100%;height: 600px">
        <SpectrumView ref="spectrum"></SpectrumView>
        <input type="file" id="myFile" />
    </div>
</template>

<script>
import SpectrumView from './components/SpectrumView.vue'
// import DSP from 'dsp.js'

export default {
    name: 'App',
    components: {
        SpectrumView
    },
    methods: {
        fftConversion: function () {
        //     function Int16Array2Float32Array (int16Array) {
        //         const data = int16Array
        //         let len = data.length
        //         let i = 0
        //         const dataAsFloat32Array = new Float32Array(len)
        //
        //         while (i < len) {
        //             dataAsFloat32Array[i] = data[i++]
        //         }
        //         return dataAsFloat32Array
        //     }
        //
        //     function Int8Array2Int16Array (int8Array) {
        //         let int16Array = new Int16Array(int8Array.length / 2)
        //         for (let i = 0; i < int8Array.length; i += 2) {
        //             let int16 = (int8Array[i + 1] << 8) | (int8Array[i] & 0xff)
        //             int16Array[i / 2] = int16
        //         }
        //         return int16Array
        //     }
        //
        //     const frameBufferSize = 16384
        //     const bufferSize = frameBufferSize / 2
        //     let signal
        //     let readData
        //     const myFile = document.querySelector('#myFile')
        //     const file = myFile.files[0]
        //     if (!file) return
        //     const reader = new FileReader()
        //
        //     if (this.filePos + bufferSize * 2 > file.size) {
        //         this.filePos = 0
        //     }
        //
        //     reader.readAsArrayBuffer(file.slice(this.filePos, this.filePos + bufferSize * 2))
        //     this.filePos += bufferSize * 2
        //
            const that = this
        //
        //     reader.onloadend = function (event) {
        //         let data1 = new Int8Array(event.target.result)
        //         readData = Int8Array2Int16Array(data1)
        //         signal = Int16Array2Float32Array(readData)
        //
        //         const fft = new DSP.FFT(bufferSize, 44100)
        //         for (let i = 0; i < signal.length; i++) {
        //             signal[i] *= DSP.WindowFunction.Hann(signal.length, i)
        //         }
        //         fft.forward(signal)
        //         for (let i = 0; i < bufferSize; i++) {
        //             fft.spectrum[i] = Math.log10(Math.sqrt(fft.spectrum[i] * fft.spectrum[i])) * 20.0
        //         }
        //         const data = []
        //         // const yData = []
        //         for (let i = 0; i < fft.spectrum.length; i += 1) {
        //             data.push([(300 + (800 - 300) / fft.spectrum.length * i), fft.spectrum[i]])
        //             // yData.push(fft.spectrum[i])
        //         }
                let childComponent = that.$refs.spectrum
        //         childComponent.setWaveData(readData)
        //         childComponent.setFftData(data)
        //     }
            childComponent.setChooseFreq(400,500)
        }
    },
    data: function () {
        return {
            filePos: 0,
            fileLength: 0,
            timer: null // 刷新定时器
        }
    },
    mounted: function () {
        let childComponent = this.$refs.spectrum
        childComponent.setChooseFreq(400,500)
        // this.timer = setInterval(() => {
        //   this.fftConversion()
        // }, 1)
    }
}
</script>

<style>
#app {
    font-family: 'Avenir', Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-align: center;
    color: #2c3e50;
    margin-top: 60px;
}
</style>
